package com.gopay.solution.service.solu.impl;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.gopay.common.domain.acps.AcpsAcctMast;
import com.gopay.common.domain.solution.LegalObjReq;
import com.gopay.common.domain.solution.SolutionRequest;
import com.gopay.common.domain.solution.SolutionResponse;
import com.gopay.common.exception.validator.GopayCheckException;
import com.gopay.common.constants.solution.SolutionTypeConstant;
import com.gopay.solution.dao.smelly.AcpsAcctMastTempQueryDAO;

/**
 * 合法交易对方
 * 
 * @SsTransLegalObjServiceImpl.java
 * @author MA HUAN
 * @2013-3-26 下午6:49:06 www.gopay.com.cn Inc.All rights reserved.
 */

@Service(value="ssTransLegalObjService")
public class SsTransLegalObjServiceImpl extends SolutionServiceImpl {
    
    @Resource(name = "acpsAcctMastTempQueryDAO")
    private AcpsAcctMastTempQueryDAO acpsAcctMastTempQueryDAO;

    @Override
    public String getSolutionType() {
        return SolutionTypeConstant.TRANS_LEGALOBJ;
    }

    public SolutionRequest beforeProcess(SolutionRequest request) throws Exception {

       

        if(StringUtils.isEmpty(request.getAcctId()) || request.getRequest() == null) {
            throw new GopayCheckException("", "输入参数错误");
        }
        
        if(!(request.getRequest() instanceof LegalObjReq)) {
            throw new GopayCheckException("", "输入参数错误");
        }
        
        LegalObjReq req = (LegalObjReq)request.getRequest();
        
        AcpsAcctMast locAcct = acpsAcctMastTempQueryDAO.find(AcpsAcctMast.class, request.getAcctId());
        if(locAcct == null) {
            throw new GopayCheckException("", "未能加载交易发起方的帐号产品类型");
        }
        
        List<String> tranObjAccts = req.getTranObjAccts();
        if(tranObjAccts == null || tranObjAccts.isEmpty()) {
        	throw new GopayCheckException("", "未能加载交易对方的帐号产品类型, 传入的交易对方为空");
        }
        
        Map<String, List<String>> tranObjAps = new Hashtable<String, List<String>>();
        List<String> accts = null;
        for(String tranObjAcct : tranObjAccts) {
        	AcpsAcctMast objAcct = acpsAcctMastTempQueryDAO.find(AcpsAcctMast.class, tranObjAcct);
        	if(objAcct == null || objAcct.getProdCode() == null) {
                throw new GopayCheckException("", "未能加载交易对方["+objAcct+"]的帐号产品类型");
            }
        	
        	accts = tranObjAps.get(objAcct.getProdCode().toString());
        	if(accts == null) {
        		accts = new ArrayList<String>();
        	}
        	
        	accts.add(tranObjAcct);
        	tranObjAps.put(objAcct.getProdCode().toString(), accts);
        }
        
        
        req.setTranLocAcct(request.getAcctId());
        req.setTranLocAp(locAcct.getProdCode().toString());
        req.setTranObjAccts(tranObjAccts);
        req.setTranObjAps(tranObjAps);
        request.setRequest(req);
        
        return request;
    }

    public SolutionResponse afterProcess(SolutionResponse response) {

        

        return response;
    }

}
